﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class BaseData_NewsTypeEdit : System.Web.UI.Page
{
    protected void Page_PreInit(object sender, EventArgs e)
    {
        Page.Theme = BLL.Common.GetWSMThemes();
    }
    private string Code
    {
        get
        {
            if (Request.QueryString["code"] != null)
            {
                return Request.QueryString["code"].ToString();
            }
            else
            {
                return "";
            }
        }
    }
    private Model.NewType Property
    {
        get
        {
            Model.NewType property = null;
            if (ViewState["Property"] != null)
            {
                property = (Model.NewType)ViewState["Property"];
            }
            return property;
        }
        set
        {
            ViewState["Property"] = value;
        }
    }
    private bool IsAdd
    {
        get
        {
            return Convert.ToBoolean(ViewState["IsAdd"]);
        }
        set
        {
            ViewState["IsAdd"] = value;
        }

    }
    protected void Page_Load(object sender, EventArgs e)
    {
        //Check.CheckPermission("200300104");
        if (!IsPostBack)
        {
            if (this.Code != "")
            {
                Label1.Text = "编辑新闻分类";
                this.Property = new BLL.NewType().GetModel(int.Parse(this.Code));
                if (this.Property != null)
                {
                    txtCode.Text = this.Property.NT_NAME;
                    txtName.Text = this.Property.NT_ORDER.ToString();
                    ckb_stop.Checked = this.Property.NT_ISSTOP == "1" ? true : false;
                    this.IsAdd = false;
                    btnAddOrUpdate.Text = "更新";
                }
                else
                {
                    Page.RegisterStartupScript("StartUp", "<script language=javascript>alert('系统中无此记录!');location.href='NewsType.aspx';</script>");
                }
            }
            else
            {
                this.IsAdd = true;
                btnAddOrUpdate.Text = "添加";
            }
            btnCancel.Attributes.Add("onclick", "if(!confirm('要取消" + btnAddOrUpdate.Text + "并返回上一页吗?'))return false;");
        }
    }
    protected void btnAddOrUpdate_Click(object sender, EventArgs e)
    {
        if (this.IsAdd == true)
        {
            if (true)//Check.CheckPermission("200300104", "00001")
            {
                if (txtCode.Text != "" && txtName.Text!="")
                {
                    Model.NewType property = new Model.NewType();
                    property.NT_NAME = txtCode.Text;
                    property.NT_ORDER = int.Parse(txtName.Text);
                    property.NT_ISSTOP = ckb_stop.Checked ? "1" : "0";
                    if (new BLL.NewType().Add(property) > 0)
                    {
                        //Share.InsertLog("200300104", "00001");
                        Page.RegisterStartupScript("StartUp", "<script language=javascript>alert('添加记录成功');location.href='NewsType.aspx';</script>");
                    }
                    else
                    {
                        Share.Alert(this.Page, "添加记录失败,请重新添加。");
                    }

                }
                else
                {
                    Page.RegisterStartupScript("StartUp", "<script language=javascript>alert('名称和序号不能为空。');</script>");
                }
            }
            else
            {
                Page.RegisterStartupScript("StartUp", "<script language=javascript>alert('无权进行此操作');location.href='NewsType.aspx';</script>");
            }
        }
        else
        {
            if (true)//Check.CheckPermission("200300104", "00002")
            {
                Property.NT_ID = int.Parse(Code);
                Property.NT_NAME = txtCode.Text;
                Property.NT_ORDER = int.Parse(txtName.Text);
                Property.NT_ISSTOP = ckb_stop.Checked ? "1" : "0";
                if (new BLL.NewType().Update(Property))
                {
                    //Share.InsertLog("200300104", "00002");
                    Page.RegisterStartupScript("StartUp", "<script language=javascript>alert('更新记录成功');location.href='NewsType.aspx';</script>");
                }
                else
                {
                    Share.Alert(this.Page, "更新记录失败,请重新编辑更新。");
                }
            }
            else
            {
                Page.RegisterStartupScript("StartUp", "<script language=javascript>alert('无权进行此操作');location.href='NewsType.aspx';</script>");
            }

        }
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("NewsType.aspx");
    }
}
